﻿using Newtonsoft.Json.Linq;
using NLog;
using System;

namespace GBT_Tool
{
    public class ConfigManage
    {
        private Logger logger = LogManager.GetCurrentClassLogger();
        Configuration config = new Configuration();
        JObject joConfig;

        string ip = ".";
        string post = "1433";
        string host = ".";
        string userId = "sa";
        string password = "Aa123456789.";
        string dbName = "MealSystem";

        public ConfigManage()
        {
            ReadAllConfig();
        }

        /// <summary>
        /// 数据库服务器是否已设置(不包含数据库名称配置)
        /// </summary>
        /// <returns></returns>
        public bool IsDbService()
        {
            bool isDbInfo = true;
            if (ip == "")
            {
                isDbInfo = false;
            }
            if (post == "")
            {
                isDbInfo = false;
            }
            if (host == "")
            {
                isDbInfo = false;
            }
            if (userId == "")
            {
                isDbInfo = false;
            }
            if (password == "")
            {
                isDbInfo = false;
            }
            return isDbInfo;
        }

        /// <summary>
        /// 数据库服务器是否已设置（需要所有数据库相关配置项都已配置才为真）
        /// </summary>
        /// <returns></returns>
        public bool IsDbAllConfig()
        {
            bool isDbInfo = true;
            if (ip == "")
            {
                isDbInfo = false;
            }
            if (post == "")
            {
                isDbInfo = false;
            }
            if (host == "")
            {
                isDbInfo = false;
            }
            if (userId == "")
            {
                isDbInfo = false;
            }
            if (password == "")
            {
                isDbInfo = false;
            }
            if (dbName == "")
            {
                isDbInfo = false;
            }
            return isDbInfo;
        }

        /// <summary>
        /// (程序首次运行时)初始化数据库配置
        /// </summary>
        public void initDb()
        {
            joConfig["ip"] = ip;
            joConfig["post"] = post;
            joConfig["host"] = ip + "," + post;
            joConfig["userID"] = userId;
            joConfig["password"] = userId;
            joConfig["dbName"] = dbName;
            config.WriteConfiguration(joConfig);
        }

        /// <summary>
        /// 设置数据库信息
        /// </summary>
        /// <param name="Name">JSON字段名</param>
        /// <param name="Value">值</param>
        private void SetDb(string Name, string Value)
        {
            switch (Name)
            {
                case "ip":
                    ip = Value;
                    break;
                case "post":
                    post = Value;
                    break;
                case "userID":
                    userId = Value;
                    break;
                case "password":
                    password = Value;
                    break;
                case "dbName":
                    dbName = Value;
                    break;
            }
            host = ip + "," + post;
            joConfig[Name] = Value;
            joConfig["host"] = host;
            config.WriteConfiguration(joConfig);
        }
        
        /// <summary>
        /// 读取所有的配置信息
        /// </summary>
        /// <returns></returns>
        private JObject ReadAllConfig()
        {
            try
            {
                joConfig = new JObject();
                //配置文件为空，则初始化配置文件内容，否则读取配置文件内容
                if (config.IsFileEmpty())
                {
                    //初始化数据库服务器
                    joConfig.Add("ip", ip);
                    joConfig.Add("post", post);
                    joConfig.Add("host", host);
                    joConfig.Add("userID", userId);
                    joConfig.Add("password", password);
                    joConfig.Add("dbName", dbName);
                }
                else
                {
                    //读取配置文件
                    joConfig = config.ReadConfiguration();
                    //数据库配置
                    ip = (string)joConfig["ip"];
                    post = (string)joConfig["post"];
                    host = (string)joConfig["host"];
                    userId = (string)joConfig["userID"];
                    password = (string)joConfig["password"];
                    dbName = (string)joConfig["dbName"];
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
            return joConfig;
        }

        /// <summary>
        /// 读取数据库配置
        /// </summary>
        /// <returns></returns>
        private JObject ReadDbConfig()
        {
            JObject joAllConfig = ReadAllConfig();
            JObject joDbConfig = new JObject
            {
                { "ip", ip },
                { "post", post },
                { "host", host },
                { "userID", userId },
                { "password", password },
                { "dbName", dbName }
            };
            return joDbConfig;
        }

        /// <summary>
        /// IP地址，或服务器名称
        /// </summary>
        public string IP
        {
            set
            {
                SetDb("ip",value);
            }
        }

        /// <summary>
        /// 端口
        /// </summary>
        public string Post
        {
            set
            {
                SetDb("post", value);
            }
        }

        /// <summary>
        /// 数据库服务器名称+端口或IP+端口
        /// </summary>
        public string Host
        {
            get
            {
                if (IsDbService())
                {
                    JObject joDbConfig = ReadDbConfig();
                }
                else
                {
                    host = ".";
                }
                return host;
            }
        }

        /// <summary>
        /// 数据库用户ID
        /// </summary>
        public string UserID
        {
            get
            {
                JObject joDbConfig = ReadDbConfig();
                return userId;
            }
            set
            {
                SetDb("userID", value);
            }
        }

        /// <summary>
        /// 数据库密码
        /// </summary>
        public string Password
        {
            get
            {
                JObject joDbConfig = ReadDbConfig();
                return password;
            }
            set
            {
                SetDb("password", value);
            }
        }

        /// <summary>
        /// 数据库名称
        /// </summary>
        public string DBName
        {
            get
            {
                JObject joDbConfig = ReadDbConfig();
                return dbName;
            }
            set
            {
                SetDb("dbName", value);
            }
        }
    }
}
